import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/http/http.dart';

import 'ManageAddress/AddManageAddress.dart';
import 'componets/addressCard.dart';

class AddressPage extends StatefulWidget {
  const AddressPage({Key key}) : super(key: key);

  @override
  _AddressPageState createState() => _AddressPageState();
}

class _AddressPageState extends State<AddressPage> {
  List addressList = [
    // {
    //   "id": 2,
    //   "userId": 1,
    //   "name": "金色传说",
    //   "addressIntro": "不得行地址",
    //   "phone": "17608706490",
    //   "province": "23",
    //   "municipality": "2",
    //   "district": "2",
    //   'absolutePosition': 1
    // },
    // {
    //   "id": 3,
    //   "userId": 1,
    //   "name": "哒哒哒哒",
    //   "addressIntro": "昆明理工大学",
    //   "phone": "13629465588",
    //   "province": "云南省",
    //   "municipality": "昆明市",
    //   "district": "冬春去",
    //   'absolutePosition': 0
    // }
  ];

  @override
  initState() {
    // print('init');
    // TODO: implement initState
    super.initState();
    // 页面加载时调用 用来开始时访问数据库
    _getaddressList();
  }

  void _getaddressList() async {
    var response = await Http.get(
        'http://xiangrikui.vaiwan.com/userAddress/selectByUserId');
// http://xiangrikui.vaiwan.com/notice/updateState?typeId=3&deviceId=10000

    // print(response.length);
    addressList = response;
    // print('更新数据');
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('我的收货地址'),
        backgroundColor: Colors.blue,
        actions: [
          InkWell(
            onTap: () {
              setState(() {});
            },
            child: InkWell(
              onTap: () {
                Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) {
                    return AddManageAddress(
                      addAddress: _addAddress,
                    ); //返回的是需要跳转单页面
                  },
                ));
              },
              child: Container(
                padding: EdgeInsets.only(top: 16, right: 14),
                child: Text(
                  '添加',
                  style: TextStyle(fontSize: 20),
                ),
              ),
            ),
          ),
        ],
      ),
      body: Container(
        color: Colors.black12,
        padding: const EdgeInsets.all(16.0),
        child: ListView(
          children: _MyshippingAddressListL(),
        ),
      ),
    );
  }

  _MyshippingAddressListL() {
    List<Widget> list = [];
    for (var i = 0; i < this.addressList.length; i++) {
      list.add(AddressCard(
        name: this.addressList[i]['name'],
        // addressIntro: this.addressList[i]['addressIntro'],
        shoppingCartSelection:
            this.addressList[i]['absolutePosition'] == 1 ? true : false,
        imageUrl: 'assets/images/common/监控.png',
        phone: this.addressList[i]['phone'],
        district: this.addressList[i]['district'],
        province: this.addressList[i]['province'],
        municipality: this.addressList[i]['municipality'],
        addressIntro: this.addressList[i]['addressIntro'],
        id: this.addressList[i]['id'],
        getaddressL: this._addressIntroRead,
        removeAddress: this._removeAddress,
      ));
    }
    return list;
  }

  void _addressIntroRead(int id) {
    _getaddressList();
  }

  void _removeAddress(int id) {
    // print('remove${id}');
    _getaddressList();
  }

  void _addAddress() {
    print('_addAddress');
    _getaddressList();
  }
}
